|
1
|
|
|
var utils = require('../utils'), |
|
2
|
|
|
Base = require('../Base'); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* A set of data that supports extension elements. |
|
6
|
|
|
* |
|
7
|
|
|
* @class |
|
8
|
|
|
* @extends Base |
|
9
|
|
|
* @param {Object} [json] |
|
10
|
|
|
*/ |
|
11
|
|
|
var ExtensibleData = function(json){ |
|
12
|
|
|
|
|
13
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
|
14
|
|
|
if(!(this instanceof ExtensibleData)){ |
|
15
|
|
|
return new ExtensibleData(json); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
|
19
|
|
|
if(ExtensibleData.isInstance(json)){ |
|
20
|
|
|
return json; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
this.init(json); |
|
|
|
|
|
|
24
|
|
|
}; |
|
25
|
|
|
|
|
26
|
|
|
ExtensibleData.prototype = Object.create(Base.prototype); |
|
27
|
|
|
|
|
28
|
|
|
ExtensibleData._gedxClass = ExtensibleData.prototype._gedxClass = 'GedcomX.ExtensibleData'; |
|
29
|
|
|
|
|
30
|
|
|
ExtensibleData.jsonProps = ['id']; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Check whether the given object is an instance of this class. |
|
34
|
|
|
* |
|
35
|
|
|
* @param {Object} obj |
|
36
|
|
|
* @returns {Boolean} |
|
37
|
|
|
*/ |
|
38
|
|
|
ExtensibleData.isInstance = function(obj){ |
|
39
|
|
|
return utils.isInstance(obj, this._gedxClass); |
|
40
|
|
|
}; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Initialize from JSON |
|
44
|
|
|
* |
|
45
|
|
|
* @param {Object} |
|
|
|
|
|
|
46
|
|
|
* @return {ExtensibleData} this |
|
47
|
|
|
*/ |
|
48
|
|
|
ExtensibleData.prototype.init = function(json){ |
|
49
|
|
|
|
|
50
|
|
|
Base.prototype.init.call(this, json); |
|
51
|
|
|
|
|
52
|
|
|
if(json){ |
|
53
|
|
|
this.setId(json.id); |
|
54
|
|
|
} |
|
55
|
|
|
return this; |
|
56
|
|
|
}; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the object's id. |
|
60
|
|
|
* |
|
61
|
|
|
* @returns {String} Id |
|
62
|
|
|
*/ |
|
63
|
|
|
ExtensibleData.prototype.getId = function(){ |
|
64
|
|
|
return this.id; |
|
65
|
|
|
}; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set the object's id. |
|
69
|
|
|
* |
|
70
|
|
|
* @param {String} id |
|
71
|
|
|
* @return {ExtensibleData} This object, for chaining. |
|
72
|
|
|
*/ |
|
73
|
|
|
ExtensibleData.prototype.setId = function(id){ |
|
74
|
|
|
this.id = id; |
|
75
|
|
|
return this; |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Export the object as JSON |
|
80
|
|
|
* |
|
81
|
|
|
* @return {Object} JSON object |
|
82
|
|
|
*/ |
|
83
|
|
|
ExtensibleData.prototype.toJSON = function(){ |
|
84
|
|
|
return this._toJSON(Base, ExtensibleData.jsonProps); |
|
85
|
|
|
}; |
|
86
|
|
|
|
|
87
|
|
|
module.exports = ExtensibleData; |